package cn.zmit.kuxi.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.entity.MyWinRecordEntity;
import cn.zmit.kuxi.holder.MyWinRecordHorld;
import cn.zmit.kuxi.request.RequestTask;
import cn.zmit.kuxi.widget.PtrHeader;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreDefaultFooterView;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;
import com.xdroid.widget.ptr.PtrDefaultHandler;
import com.xdroid.widget.ptr.PtrFrameLayout;
import com.xdroid.widget.ptr.PtrHandler;

/** * @author @parameter * @return */
public class WinningRecordActivity extends BaseActivity {
	@ViewInject(R.id.listView_win_record)
	private ListView listView_win_record;
	private String userId;
	private ListViewDataAdapter<MyWinRecordEntity> myWinAdapter;
	// 下拉刷新
	@ViewInject(R.id.ptr_view)
	private PtrFrameLayout ptr_view;
	private PtrHeader ptrHeader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("中奖记录", true);
		setContentView(R.layout.activity_winning_record);
		ViewUtils.inject(this);
		initView();
		initPtr();
		initLoadMore();

	}

	/**
	 * 初始化下拉刷新
	 */
	private void initPtr() {
		/* 创建自定义刷新头部view */
		ptrHeader = new PtrHeader(this);
		/* 设置刷新头部view */
		ptr_view.setHeaderView(ptrHeader);
		/* 设置回调 */
		ptr_view.addPtrUIHandler(ptrHeader);
		/* 横向拉动时，禁止下拉 */
		ptr_view.disableWhenHorizontalMove(true);
		// 设置监听
		ptr_view.setPtrHandler(new PtrHandler() {

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				frame.postDelayed(new Runnable() {

					@Override
					public void run() {
						mPage = 1;
						myWinAdapter.removeAll();
						getDate();
					}

				}, 500);
			}

			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						listView_win_record, header);
			}
		});
		/* 延时100hao秒,自动刷新 */
		ptr_view.postDelayed(new Runnable() {

			@Override
			public void run() {
				ptr_view.autoRefresh();
			}
		}, 100);
	}

	private void initView() {
		myWinAdapter = new ListViewDataAdapter<MyWinRecordEntity>();
		myWinAdapter.setViewHolderClass(this, MyWinRecordHorld.class);
		listView_win_record.setAdapter(myWinAdapter);
	}

	private int mPage = 1;
	LoadMoreDefaultFooterView mFooterView;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mFooterView = mLoadMoreListViewContainer.useDefaultFooterRe(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
					getDate();
				}
			}
		});

	}

	// qingqiu
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载

			JsonData jsonData = JsonData.create(response);
			JsonData data = jsonData.optJson("my_win_record");
			int now_time = jsonData.optInt("now_time");
			if (jsonData != null && jsonData.length() > 0) {
				if (data.length() == 8) {
					mHasMore = true; // 有更多数据
				} else {
					mIsEmpty = false; // 有数据
					mHasMore = false; // 无更多数据
				}
				if (data != null && data.length() > 0) {
					for (int i = 0; i < data.length(); i++) {
						JsonData itemData = data.optJson(i);
						String my_buy = itemData.optString("my_buy_count");
						String gp_id = itemData.optString("gp_id");
						String title = itemData.optString("phase");
						String lucky_number = itemData
								.optString("lucky_number");
						String time = itemData.optString("announceOn");
						int oldtime = itemData.optInt("unix_announceOn");
						String name = itemData.optString("name");
						String pic = itemData.optString("pic");
						String content = itemData.optString("content");
						MyWinRecordEntity myWinRecordEntity = new MyWinRecordEntity(
								my_buy, gp_id, title, lucky_number, time, name,
								content, pic, oldtime, now_time);
						myWinAdapter.append(myWinRecordEntity);
					}
				} else {
					mIsEmpty = true; // 空数据
					mHasMore = false; // 无更多数据
				}
				mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); //
			}
			ptr_view.refreshComplete();
		}

		@Override
		public void onRequestPrepare() {
			// TODO Auto-generated method stub
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			// TODO Auto-generated method stub
			super.onRequestFailed(error);
		}
	}

	private void getDate() {
		Boolean haslogin = PreferenceHelper.readBoolean(this,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
				false);
		if (haslogin) {
			userId = PreferenceHelper.readString(this,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", userId);
			RequestTask.getInstance().doWinRecord(this, mPage + "", map,
					new onRequestListener());
		}
	}

	@Override
	protected void onResume() {
		initPtr();
		super.onResume();

	}
}
